<template>
  <el-card
    class="box-card"
    style="background-color: #fff; margin-left: 1%; width: 98%"
  >
    <div
      style="
        display: flex;
        justify-content: space-between;
        border-bottom: 1px solid #ebeef5;
      "
    >
      <p>店铺管理</p>
      <el-button
        @click="clickSave"
        style="margin-bottom: 4px"
        size="mini"
        type="primary"
        >保存</el-button
      >
    </div>
    <div>
      <!-- 店铺名称 -->
      <p style="display: flex; margin-top: 20px">
        <span style="margin-right: 14px; line-height: 36px">店铺名称</span>
        <el-input
          style="width: 300px"
          placeholder="请输入内容"
          v-model="storeData.name"
          clearable
        ></el-input>
      </p>

      <!-- 店铺公告 -->
      <p style="display: flex; margin-top: 20px">
        <span style="margin-right: 14px; line-height: 36px">店铺公告</span>
        <el-input
          style="width: 300px"
          type="textarea"
          :rows="6"
          placeholder="请输入内容"
          v-model="storeData.bulletin"
        >
        </el-input>
      </p>

      <!-- 店铺头像 -->
      <p style="display: flex; margin-top: 20px">
        <span style="margin-right: 14px; line-height: 36px">店铺头像</span>
        <el-upload
          :show-file-list="false"
          action="http://127.0.0.1:5000/shop/upload"
          :on-success="avatarSuccess"
        >
          <img style="width: 150px; height: 150px" :src="storeData.avatar" />
        </el-upload>
      </p>

      <!-- 店铺图片 -->
      <p style="display: flex; margin-top: 20px">
        <span style="margin-right: 14px; line-height: 36px">店铺图片</span>
        <el-upload
          action="http://127.0.0.1:5000/shop/upload"
          :file-list="callbackimgs"
          list-type="picture-card"
          :on-success="picsSucc"
          :on-remove="picsRemove"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
      </p>

      <!-- 配送费 -->
      <p style="display: flex; margin-top: 20px">
        <span style="line-height: 36px; width: 62px; text-align: right"
          >配送费</span
        >
        <el-input
          style="width: 300px; margin-left: 14px"
          placeholder="请输入内容"
          v-model="storeData.deliveryPrice"
          clearable
        ></el-input>
      </p>

      <!-- 配送时间 -->
      <p style="display: flex; margin-top: 20px">
        <span style="margin-right: 14px; line-height: 36px">配送时间</span>
        <el-input
          style="width: 300px"
          placeholder="请输入内容"
          v-model="storeData.deliveryTime"
          clearable
        ></el-input>
      </p>

      <!-- 配送描述 -->
      <p style="display: flex; margin-top: 20px">
        <span style="margin-right: 14px; line-height: 36px">配送描述</span>
        <el-input
          style="width: 300px"
          placeholder="请输入内容"
          v-model="storeData.description"
          clearable
        ></el-input>
      </p>

      <!-- 点评评分 -->
      <p style="display: flex; margin-top: 20px">
        <span style="margin-right: 14px; line-height: 36px">点评评分</span>
        <el-input
          style="width: 300px"
          placeholder="请输入内容"
          v-model="storeData.score"
          disabled
        ></el-input>
      </p>

      <!-- 销量 -->
      <p style="display: flex; margin-top: 20px">
        <span style="line-height: 36px; width: 62px; text-align: right"
          >销量</span
        >
        <el-input
          style="width: 300px; margin-left: 14px"
          placeholder="请输入内容"
          v-model="storeData.sellCount"
          disabled
        ></el-input>
      </p>

      <!-- 添加活动 -->
      <p style="display: flex; margin-top: 20px">
        <span style="margin-right: 14px; line-height: 36px">添加活动</span>
        <el-input
          style="width: 300px"
          placeholder="请输入内容"
          v-model="newActiveName"
          clearable
        ></el-input>
        <el-button
          @click="clickAddActive"
          type="primary"
          style="margin-left: 20px"
          size="mini"
          >添加</el-button
        >
      </p>
      <!-- 活动 -->
      <p style="display: flex; margin-top: 20px">
        <span style="line-height: 36px; width: 62px; text-align: right"
          >活动</span
        >
        <el-checkbox-group
          style="
            display: flex;
            flex-wrap: wrap;
            width: 300px;
            margin-left: 14px;
            line-height: 34px;
          "
          v-model="selectCheck"
        >
          <el-checkbox
            v-for="item in activeList"
            :key="item"
            :label="item"
          ></el-checkbox>
        </el-checkbox-group>
      </p>

      <!-- 营业时间 -->
      <p style="display: flex; margin-top: 20px">
        <span style="margin-right: 14px; line-height: 36px">营业时间</span>
        <el-time-picker
          is-range
          v-model="storeData.date"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          placeholder="选择时间范围"
          value-format="yyyy-MM-dd HH:mm:ss"
        >
        </el-time-picker>
      </p>
    </div>
  </el-card>
</template>

<script>
import { info, edit } from "@/apis/store";
export default {
  data() {
    return {
      storeData: {},
      callbackimgs: [], //回填照片墙的照片数组
      newActiveName: "", //添加活动
      activeList: [], //活动
      selectCheck: [], //选中的活动数组
      newAvatar: "", //店铺新头像
      imgsArr: [], //照片墙数组(原始+新上传)
    };
  },
  created() {
    this.reqData();
    // console.log(this.selectCheck);
  },
  methods: {
    reqData() {
      info().then((res) => {
        // console.log(res);
        this.storeData = res.data.data;
        //初始化回填照片墙数组
        this.callbackimgs = this.storeData.pics.map((obj) => {
          return {
            name: obj.substring(obj.lastIndexOf("/") + 1),
            url: obj,
          };
        });

        //如果修改数据时没有修改头像,初始化赋值
        this.newAvatar = this.storeData.avatar.substring(
          this.storeData.avatar.lastIndexOf("/") + 1
        );

        //把原本就存在的图片放入照片墙数组中
        this.imgsArr = this.storeData.pics.map((obj) =>
          obj.substring(obj.lastIndexOf("/") + 1)
        );

        this.activeList = [...this.activeList, ...this.storeData.supports];
        // console.log(this.activeList);
      });
    },

    //添加新活动
    clickAddActive() {
      this.activeList.push(this.newActiveName);
    },

    //头像上传成功
    avatarSuccess(res, file) {
      this.storeData.avatar = URL.createObjectURL(file.raw);
      console.log(res, file);
      //保存时发送给后台的数据
      this.newAvatar = res.imgUrl;
    },

    //照片墙上传成功
    picsSucc(res) {
      // console.log(res);
      this.imgsArr.push(res.imgUrl);
    },

    //照片墙删除成功
    picsRemove(file) {
      // console.log(file);
      let imgName = file.response ? file.response.imgUrl : file.name;
      for (let i = 0; i < this.imgsArr.length; i++) {
        if (this.imgsArr[i] == imgName) {
          this.imgsArr.splice(i, 1);
          break;
        }
      }
    },

    //保存
    clickSave() {
      //拷贝，把要渲染数据和参数数据分开，防止修改参数，影响界面渲染
      let params = JSON.parse(JSON.stringify(this.storeData));
      params.avatar = this.newAvatar; //头像
      params.pics = JSON.stringify(this.imgsArr); //照片墙
      params.supports = JSON.stringify(this.selectCheck); //选中的活动
      params.date = JSON.stringify(params.date); //营业时间

      edit(params).then((res) => {
        this.$message({
          message: res.data.msg,
          type: "success",
        });
      });
      // console.log(params);
    },
  },
};
</script>

<style lang="less" scoped>
.icon {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
  display: flex;
  justify-content: space-between;
}
</style>